Imports DotNetNuke.Framework.Providers
Imports DotNetNuke.Common.Utilities
Imports DotNetNuke.Common.Utilities.Config
Imports System.StringComparer
Imports System.Data
Imports System.Data.SqlClient

Public Class ProductGroupController
#Region "Methods"
    Public Sub New()
        Dim provider As Provider = CType(Me._providerConfiguration.Providers.Item(Me._providerConfiguration.DefaultProvider), Provider)
        Me._objectQualifier = provider.Attributes.Item("objectQualifier")
        If (Me._objectQualifier <> String.Empty And Not Me._objectQualifier.EndsWith("_")) Then
            Me._objectQualifier = (Me._objectQualifier & "_")
        End If
        Me._databaseOwner = provider.Attributes.Item("databaseOwner")
        If (Me._databaseOwner <> String.Empty And Not Me._databaseOwner.EndsWith(".")) Then
            Me._databaseOwner = (Me._databaseOwner & ".")
        End If
    End Sub

   

    Public Sub DeleteProductGroup(ByVal ProductGroupId As Integer)
        Try
            Dim connection As New SqlConnection(Me.ConnectionString)
            Dim command As New SqlCommand(DatabaseOwner & ObjectQualifier & "activeapc_dnn_ProductGroups_Delete", connection)
            command.CommandType = CommandType.StoredProcedure
            command.Parameters.Add("@ProductGroupId", SqlDbType.Int).Value = ProductGroupId
            connection.Open()
            command.ExecuteNonQuery()
            connection.Close()
            command.Dispose()
            connection.Dispose()
        Catch ex As Exception

        End Try
    End Sub

    Private Function GetNull(ByVal Field As Object) As Object
        Return Null.GetNull((Field), DBNull.Value)
    End Function

    Public Function GetProductGroupsByPortalId(ByVal PortalId As Integer) As IDataReader
        Try
            Dim connection As New SqlConnection(Me.ConnectionString)
            Dim command As New SqlCommand(DatabaseOwner & ObjectQualifier & "activeapc_dnn_ProductGroups_GetByPortalId", connection)
            command.CommandType = CommandType.StoredProcedure
            command.Parameters.Add("@PortalId", SqlDbType.Int).Value = PortalId
            connection.Open()
            Return command.ExecuteReader(CommandBehavior.CloseConnection)
        Catch ex As Exception
            Return Nothing
        End Try
    End Function

    Public Function GetProductGroupsByModuleId(ByVal ModuleId As Integer) As IDataReader
        Try
            Dim connection As New SqlConnection(Me.ConnectionString)
            Dim command As New SqlCommand(DatabaseOwner & ObjectQualifier & "activeapc_dnn_ProductGroups_GetByModuleId", connection)
            command.CommandType = CommandType.StoredProcedure
            command.Parameters.Add("@ModuleId", SqlDbType.Int).Value = ModuleId
            connection.Open()
            Return command.ExecuteReader(CommandBehavior.CloseConnection)
        Catch ex As Exception
            Return Nothing
        End Try
    End Function

    Public Function GetProductGroupsCommentCount(ByVal ProductGroupId As Integer) As Integer
        Try
            Dim connection As New SqlConnection(Me.ConnectionString)
            Dim command As New SqlCommand(DatabaseOwner & ObjectQualifier & "activeapc_dnn_ProductGroup_GetNoteCount", connection)
            command.CommandType = CommandType.StoredProcedure
            command.Parameters.Add("@ProductGroupId", SqlDbType.Int).Value = ProductGroupId
            connection.Open()
            Dim i As Integer = CInt(command.ExecuteScalar)
            connection.Close()
            Return i
        Catch ex As Exception
            Return Nothing
        End Try
    End Function

    Public Function SaveProductGroup(ByVal pg As ProductGroupInfo) As Integer
        Try
            Dim connection As New SqlConnection(Me.ConnectionString)
            Dim command As New SqlCommand(DatabaseOwner & ObjectQualifier & "activeapc_dnn_ProductGroups_Save", connection)
            command.CommandType = CommandType.StoredProcedure
            Dim parameters As SqlParameterCollection = command.Parameters
            parameters.Add("@ProductGroupId", SqlDbType.Int).Value = pg.ProductGroupId
            parameters.Add("@ProductGroup", SqlDbType.NVarChar, 2000).Value = pg.ProductGroup
            parameters.Add("@PortalId", SqlDbType.Int).Value = pg.PortalId
            parameters.Add("@ModuleId", SqlDbType.Int).Value = pg.ModuleId
            parameters = Nothing
            connection.Open()
            pg.ProductGroupId = CInt(command.ExecuteScalar)
            connection.Close()
            command.Dispose()
            connection.Dispose()
            Return pg.ProductGroupId
        Catch ex As Exception
            Return Nothing
        End Try
    End Function
#End Region

#Region "Public Properties"
    Public ReadOnly Property ConnectionString() As String
        Get
            Return Me._connectionString
        End Get
    End Property

    Public ReadOnly Property DatabaseOwner() As String
        Get
            Return Me._databaseOwner
        End Get
    End Property

    Public ReadOnly Property ObjectQualifier() As String
        Get
            Return Me._objectQualifier
        End Get
    End Property
#End Region

#Region "Private Members"
    Private _connectionString As String = Config.GetConnectionString
    Private _databaseOwner As String
    Private _objectQualifier As String
    Private _providerConfiguration As ProviderConfiguration = ProviderConfiguration.GetProviderConfiguration("data")
    Private Const ProviderType As String = "data"
#End Region
End Class

 
